package io.github.junxworks.qt.modules.mm.controller;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import io.github.junxworks.ep.core.Result;
import io.github.junxworks.ep.core.utils.PageUtils;
import io.github.junxworks.ep.sys.annotations.EpLog;
import io.github.junxworks.ep.sys.constants.RecordStatus;
import io.github.junxworks.junx.core.util.StringUtils;
import io.github.junxworks.qt.modules.mm.dto.MmVariableCondition;
import io.github.junxworks.qt.modules.mm.dto.MmVariableDto;
import io.github.junxworks.qt.modules.mm.service.MmVariableService;
import io.github.junxworks.qt.modules.mm.vo.MmVariableVo;

/**
 * mm_variable Entity Controller Class
 *
 * @since 2022-3-21 14:52:38 Generated by JunxEP
 */
@RestController
@RequestMapping("/mm/variables")
public class MmVariableController {
	@Autowired
	private MmVariableService mmVariableService;

	/**
	 * Query list.
	 *
	 * @param condition the condition
	 * @return the result
	 */
	@GetMapping()
	public Result queryMmVariableList(MmVariableCondition condition) {
		//查询列表数据
		PageUtils.setPage(condition);
		return Result.ok(new PageInfo<>(mmVariableService.queryList(condition)));
	}

	/**
	 * Save .
	 *
	 * @param dto the dto
	 * @return the result
	 */
	@EpLog("模型管理-变量管理-保存变量")
	@PostMapping()
	public Result saveMmVariable(@RequestBody MmVariableDto dto) {
		mmVariableService.save(dto);
		return Result.ok();
	}

	@GetMapping("/refresh")
	public Result refreshMmVariable() {
		List<MmVariableVo> vars = mmVariableService.queryList(new MmVariableCondition());
		for (MmVariableVo v : vars) {
			MmVariableDto dto = new MmVariableDto();
			BeanUtils.copyProperties(v, dto);
			mmVariableService.save(dto);
		}
		return Result.ok();
	}

	/**
	 * Query entity by id.
	 *
	 * @param id the id
	 * @return the result
	 */
	@GetMapping("/{id}")
	public Result queryMmVariableEntityById(@PathVariable("id") Long id) {
		return Result.ok(mmVariableService.queryMmVariableById(id));
	}

	/**
	 * Delete.
	 *
	 * @param id the id
	 * @return the result
	 */
	@EpLog("模型管理-变量管理-删除变量")
	@DeleteMapping("/{id}")
	public Result deleteMmVariable(@PathVariable("id") Long id) {
		String msg = mmVariableService.checkVarCanDelete(id);
		if (StringUtils.notNull(msg)) {
			return Result.error(msg);
		}
		MmVariableDto dto = new MmVariableDto();
		dto.setId(id);
		dto.setStatus(RecordStatus.DELETED.getValue());
		mmVariableService.save(dto);
		return Result.ok();
	}
}